// pages/shoppingCart/shoppingCart.js

let api = require("../../utils/api.js");
let util = require("../../utils/util.js");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsArr: [],
    lastX: 0,          //滑动开始x轴位置
    lastY: 0,          //滑动开始y轴位置
    text: "没有滑动",
    currentGesture: 0, //标识手势
    slide: "",// 动画
    isSlide: "", // 判断是否滑动
    totalPrice: '0.00', // 总价格
    now_index: 0,
    isAllSel: true,// 全选判断
    obj: 0
  },
  // 左右滑动
  handletouchmove(event) {
    // console.log(event)
    var currentX = event.touches[0].pageX
    var currentY = event.touches[0].pageY
    var tx = currentX - this.data.lastX
    var ty = currentY - this.data.lastY
    var text = ""
    //左右方向滑动
    if (Math.abs(tx) > Math.abs(ty)) {
      if (tx < 0) {
        text = "向左滑动";
        this.setData({
          slide: this.ani,
        })
      }
      else if (tx > 0) {
        text = "向右滑动"
        this.setData({
          slide: this.ani2,
        })
      }

    }
    // console.log(this.data.text)
    //将当前坐标进行保存以进行下一次计算
    this.data.lastX = currentX
    this.data.lastY = currentY
    this.setData({
      text: text,
    });
  },
  //滑动开始事件
  handletouchtart: function (event) {
    console.log(event.currentTarget.dataset.index)
    this.setData({
      isSlide: event.currentTarget.dataset.index
    })
    this.data.lastX = event.touches[0].pageX
    this.data.lastY = event.touches[0].pageY
  },
  //滑动结束事件
  handletouchend: function (event) {
    this.data.currentGesture = 0;
    this.setData({
      text: "没有滑动",
    });
  },
  // 点击删除
  del(e) {
    console.log(e.currentTarget.dataset.id)
    let index = e.currentTarget.dataset.index;
    this.data.goodsArr.splice(index, 1);
    this.setData({
      goodsArr: this.data.goodsArr
    })
    // 删除接口
    util.promise_ajax(api.goodsCartDel, "post", {
      productIds: e.currentTarget.dataset.id.toString()
    }).then((res) => {
      console.log(res);
    })
    this.goodsAll();
  },
  // 点击添加或删除数量
  computedCount(e) {
    // console.log(e)
    // console.log(e.target.dataset.type);
    // console.log(e.target.dataset.index);
    // console.log(this.data.goodsArr[e.target.dataset.index].number)
    let goodsNumber = this.data.goodsArr[e.target.dataset.index];
    if (e.target.dataset.type === "+") {
      goodsNumber.number++;
    } else {
      goodsNumber.number < 2 ? goodsNumber.number : goodsNumber.number--;
    }

    this.setData({
      goodsArr: this.data.goodsArr
    })

    console.log(this.data.goodsArr)
    // 更新购物车的数量
    util.promise_ajax(api.goodsUpdate, "post", {
      id: goodsNumber.id,
      goodsId: goodsNumber.goods_id,
      number: goodsNumber.number,
      productId: goodsNumber.product_id
    }).then((res) => {
      console.log(res);
      this.setData({
        goodsArr: res.data.data.cartList
      })
    })
    // 计算价格
    this.price();
    // 获取商品件数
    this.goodsAll();
  },
  // 点击选中单个商品
  chooseGoods(e) {
    console.log(e)
    console.log(e.detail.value);
    let goodsValue = e.detail.value;
    e.detail.value.forEach((val) => {
      this.setData({
        now_index: val
      })
    })
    this.data.goodsArr.forEach((val, index) => {
      // 判断是否全部商品已经被选到
      this.setData({
        isAllSel: goodsValue.length == this.data.goodsArr.length
      })
      // 判断value里面的有没有goodsArr的值的下标
      if (goodsValue.indexOf(index.toString()) != -1) {
        this.setData({
          ["goodsArr[" + index + "].checked"]: 1,

        })
      } else {
        this.setData({
          ["goodsArr[" + index + "].checked"]: 0,

        })
      }
      // console.log(this.data.goodsArr[index].isChecked)product_id
    })

    this.setData({
      goodsArr: this.data.goodsArr
    })
    // console.log(this.data.Checked)
    // console.log(this.data.goodsArr)
    // console.log(this.data.goodsArr[this.data.now_index].product_id)
    let proId = this.data.goodsArr[this.data.now_index].product_id;
    let checked = this.data.goodsArr[this.data.now_index].checked;
    // 数据请求
    util.promise_ajax(api.goodsCartChecked, "post", { productIds: proId, isChecked: checked }).then((res) => {
      console.log(res)
    })

    this.price();
  },
  // 计算价格
  price(e) {
    let total = 0;
    this.data.goodsArr.forEach((val, index) => {
      if (val.checked == 1) {
        total += val.retail_price * val.number;
      }

    })
    this.setData({
      totalPrice: total.toFixed(2)
    })
  },
  // 全选
  selAll(e) {
    let isSel = this.data.isAllSel;

    this.data.goodsArr.forEach((val) => {
      val.checked = !isSel;
    })
    this.setData({
      goodsArr: this.data.goodsArr,
      isAllSel: !isSel
    })

    //计算价格
    this.price();
  },
  goodsAll() {
    // 获取购物车商品件数
    util.promise_ajax(api.goodscount, "get").then((res) => {
      console.log(res.data.data.cartTotal.goodsCount)
      // this.data.obj = res.data.data.cartTotal.goodsCount;
      wx.setTabBarBadge({
        index: 2,
        text: res.data.data.cartTotal.goodsCount.toString()
      })
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    console.log(options.id);


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 动画
    this.ani = wx.createAnimation({
      duration: 500,
      timingFuncion: "linear"
    })
    // this.ani.left(45).step();
    this.ani.left("-155rpx").step();
    // 动画
    this.ani2 = wx.createAnimation({
      duration: 500,
      timingFuncion: "linear"
    })
    // this.ani.left(45).step();
    this.ani2.left("0").step();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 读取购物车的数据
    util.promise_ajax(api.goodsCart, "get").then((res) => {

      res.data.data.cartList.forEach((val, index) => {
        val.isChecked = false;
      });
      console.log(res.data.data.cartList);
      this.setData({
        goodsArr: res.data.data.cartList,
      })
      console.log("goodsarr", this.data.goodsArr)
      // 计算价格
      this.price();

      this.goodsAll();

    })
    this.setData({
      slide: this.ani2,
    })




  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})